package com.wy.business.market.market.services;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.wy.business.market.home.pojos.TMarketNews;
import com.wy.business.market.market.pojos.TMarketFunctionRemarkInfo;
import com.wy.framework.constants.DBConstant;
import com.wy.framework.constants.RtnCodeConstant;
import com.wy.framework.dao.ErrorSqlException;
import com.wy.framework.pojos.TSysFunction;
import com.wy.framework.services.AbstractBaseService;
import com.wy.component.dataConvert.DateUtil;

@Service
public class MarketAppsManageService extends AbstractBaseService {

	private static Logger logger = LoggerFactory.getLogger(MarketAppsManageService.class);
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Map> getAppList(String userId)throws ErrorSqlException{
		List param=new ArrayList();
		List<Map> list;
		if(StringUtils.isEmpty(userId))
			list=dataBaseDao.queryForList("t_sys_function.queryAppList");
		else{
			List params = new ArrayList();
			params.add(userId);
			params.add(userId);
			params.add(userId);
			list=dataBaseDao.queryForList("t_sys_function.queryAppListByUserId",params);
		}
		return list;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Map> getAppType()throws ErrorSqlException{
		List<Map> list=dataBaseDao.queryForList("t_sys_function.queryAppType");
		return list;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Map> getAppDetailForFunctionId(String functionId)throws ErrorSqlException{
		List<Map> list=new ArrayList();
		List param=new ArrayList();
		param.add(functionId);
		param.add(functionId);
		list=dataBaseDao.queryForList("t_sys_function.queryAppDetailForFunctionId",param);
		return list;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Map> getAppRemarkList(String functionId,int pageNo,int pageSize)throws ErrorSqlException{
	    List param=new ArrayList();
	    param.add(functionId);
	    param.add(functionId);
	    param.add(functionId);
	    param.add(pageNo*pageSize);
	    param.add(pageNo*pageSize-pageSize+1);
		List<Map> list=dataBaseDao.queryForList("t_market_functionremarkinfo.queryRemarkList", param);
		return list;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String saveRemark(TMarketFunctionRemarkInfo tMarketFunctionRemarkInfo)throws ErrorSqlException{
		List<Map> list=dataBaseDao.queryForList("t_sys_userinfo.loadUserName",tMarketFunctionRemarkInfo.getUserId());
		tMarketFunctionRemarkInfo.setOperatorName((String)list.get(0).get("username"));
		tMarketFunctionRemarkInfo.setRemarkId(UUID.randomUUID().toString());
		tMarketFunctionRemarkInfo.setHotTypeId("0");
		tMarketFunctionRemarkInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
		tMarketFunctionRemarkInfo.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		tMarketFunctionRemarkInfo.setIsAvailable(DBConstant.DB_STATUS_ABLCE);
		dataBaseDao.save(tMarketFunctionRemarkInfo);
		return RtnCodeConstant.RTN_CODE_SUCCESS;
	}

	public String updateViews(String functionId) throws ErrorSqlException {
		TSysFunction tSysFunction = (TSysFunction) dataBaseDao.load(TSysFunction.class, functionId);
		tSysFunction.setAppDownCtn(tSysFunction.getAppDownCtn()+1);
		dataBaseDao.update(tSysFunction);
		return RtnCodeConstant.RTN_CODE_SUCCESS;
	}
	
}
